package com.itheima.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.goods.mapper.GoodsDao;
import com.itheima.goods.pojo.Goods;
import com.itheima.goods.service.GoodsService;
import entity.PageResult;
import entity.Result;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author wang
 * @description
 * @date 2021/7/29 13:04
 */
@Service("goodsService")
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
public class GoodsServiceImpl extends ServiceImpl<GoodsDao, Goods> implements GoodsService {
    @Override
    public List<Goods> findAll() {
        return this.list();
    }

    @Override
    public PageResult<Goods> findPage4Get(Integer page, Integer size) {
        //设置分页参数
        IPage<Goods> pageInfo = new Page<>(page, size);
        //分页查询
        LambdaQueryWrapper<Goods> wrapper = Wrappers.lambdaQuery(Goods.class);
        wrapper.orderByDesc(Goods::getId);
        this.page(pageInfo, wrapper);
        //封装至PageResult
        PageResult<Goods> pageResult = new PageResult<>();
        pageResult.setRows(pageInfo.getRecords());
        pageResult.setTotal(pageInfo.getTotal());
        return pageResult;
    }

    @Override
    public PageResult<Goods> findPage4Post(Integer page, Integer size, Goods goods) {
        //设置分页参数
        IPage<Goods> pageInfo = new Page<>(page, size);
        //分页查询
        LambdaQueryWrapper<Goods> wrapper = Wrappers.lambdaQuery(Goods.class);
        if (goods.getSn() != null && !"".equals(goods.getSn().trim())) {
            wrapper.eq(Goods::getSn, goods.getSn());
        }
        if (goods.getName() != null  && !"".equals(goods.getName().trim())) {
            wrapper.like(Goods::getName, goods.getName());
        }
        if (goods.getCaption() != null  && !"".equals(goods.getCaption().trim())) {
            wrapper.like(Goods::getCaption, goods.getCaption());
        }
        if (goods.getPrice() != null) {
            wrapper.eq(Goods::getPrice, goods.getPrice());
        }
        if (goods.getNum() != null) {
            wrapper.eq(Goods::getNum, goods.getNum());
        }
        if (goods.getImage() != null  && !"".equals(goods.getImage().trim())) {
            wrapper.eq(Goods::getImage, goods.getImage());
        }
        if (goods.getImages() != null && !"".equals(goods.getImages().trim())) {
            wrapper.eq(Goods::getImages,goods.getImages());
        }
        if (goods.getCategory() != null && !"".equals(goods.getCategory().trim())) {
            wrapper.like(Goods::getCategory,goods.getCategory());
        }
        if (goods.getBrand() != null && !"".equals(goods.getBrand().trim())) {
            wrapper.like(Goods::getBrand,goods.getBrand());
        }
        if (goods.getSpec() != null && !"".equals(goods.getSpec().trim())) {
            wrapper.eq(Goods::getSpec,goods.getSpec());
        }
        if (goods.getSaleNum() != null) {
            wrapper.eq(Goods::getSaleNum,goods.getSaleNum());
        }
        if (goods.getCommentNum() != null) {
            wrapper.eq(Goods::getCommentNum,goods.getCommentNum());
        }
        if (goods.getIsMarketable() != null && !"".equals(goods.getIsMarketable().trim())) {
            wrapper.eq(Goods::getIsMarketable,goods.getIsMarketable());
        }
        if (goods.getStatus() != null && !"".equals(goods.getStatus().trim())) {
            wrapper.eq(Goods::getStatus,goods.getStatus());
        }
        if (goods.getCreatetime() != null) {
            wrapper.eq(Goods::getCreatetime,goods.getCreatetime());
        }
        if (goods.getUpdatetime() != null) {
            wrapper.eq(Goods::getUpdatetime,goods.getUpdatetime());
        }
        wrapper.orderByDesc(Goods::getId);
        this.page(pageInfo, wrapper);
        //封装至PageResult
        PageResult<Goods> pageResult = new PageResult<>();
        pageResult.setRows(pageInfo.getRecords());
        pageResult.setTotal(pageInfo.getTotal());
        return pageResult;
    }

    @Override
    public Goods findById(Integer id) {
        return this.getById(id);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,readOnly = false)
    public boolean deleteById(Integer id) {
        return this.removeById(id);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,readOnly = false)
    public boolean add(Goods goods) {
        return this.save(goods);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,readOnly = false)
    public boolean updateGoods(Goods goods, Integer id) {
        goods.setId(id);
        return this.updateById(goods);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,readOnly = false)
    public Result buy(Integer goodId, Integer buyNum) {
        // 查询商品
        Goods goods = this.getById(goodId);
        if (goods == null) return new Result(false,"该商品不存在");
        // 检查库存
        if(goods.getNum() < buyNum){
            return new Result(false,"商品库存不足");
        }
        // 扣减库存
        goods.setNum(goods.getNum() - buyNum);
        this.updateById(goods);
        // 返回
        return new Result(true,"商品扣减库存成功");
    }
}
